﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class XfExtRecord : BiffRecord
    {
        public override string ToString()
        {
            return base.ToString() + " " + this._ixfe;
        }
        private FrtHeader _frtHeader;
        private ushort _reserved1;
        private ushort _ixfe;
        private ushort _reserved2;
        private List<ExtProp> _rgExt;
        public XFRecord XFRecord;

        
        public XfExtRecord(List<ExtProp> properties): this()
        {
            _rgExt = properties;
        }

        public XfExtRecord()
        {
            this._frtHeader._rt = 0x087D;
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            _frtHeader.Read(context.Reader);
            Contract.Assert(_frtHeader._rt == 0x087D);
            _reserved1 = context.ReadUInt16();
            _ixfe = context.ReadUInt16();
            _reserved2 = context.ReadUInt16();
            ushort cexts = context.ReadUInt16();
            _rgExt = new List<ExtProp>();
            for (int i = 0; i < cexts; i++)
            {
                ExtProp s = new ExtProp();
                s.Read(context.Reader);
                _rgExt.Add(s);
            }
            if (_ixfe < context.XFRecords.Count)
            {
                XFRecord xfRecord = context.XFRecords[_ixfe];
                xfRecord.ExtRecord = this;
            }
        }

        

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.XFExt; }
        }

        public IEnumerable<ExtProp> Properties
        {
            get { return this._rgExt; }
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            Contract.Assert(this.XFRecord != null);
            Contract.Assert(_frtHeader._rt == 0x087D);
            this._ixfe = (ushort) context.XFRecords.IndexOf(this.XFRecord); //\\OK for performance
            _frtHeader.Write(context);
            //\\Debug.WriteLine(string.Format("writing extRecord for {0} ", XFRecord.GetHashCode()));
            //\\Debug.WriteLine(string.Format("writing extRecord for {0} count = {1}", _ixfe, _rgExt.Count));
            context.Write(_reserved1);
            context.Write(_ixfe);
            context.Write(_reserved2);
            context.Write((ushort)_rgExt.Count);
            foreach (var extProp in _rgExt)
            {
                extProp.Write(context.Writer);
            }

        }

        public T GetPropertyValue<T>(ExtPropType type) where T : ExtPropValueBase
        {
            foreach (var prop in Properties)
            {
                if (prop.extType == type)
                    return (T)prop.value;

            }
            return null;
        }
    }
}